import { changeUserInfo } from '@/api/userApi.js'
export default {
  data(){
    return{
      bir:{
        y:null,
        m:null,
        d:null,
      },
    }
  },
  methods:{
    async birchange(){
      if(!this.change.bir){
        const bir = new Date(this.info.bir)
        this.bir = {
          y:bir.getFullYear(),
          m:bir.getMonth()+1,
          d:bir.getDate(),
        }
        this.change.bir = true
      }else{
        const { y,m,d } = this.bir
        const bir = `${y}-${m}-${d}`
        const { age,constellation,info } = this
        const list = []//POST_DATA
        if(bir != info.bir) list.push({key:'bir',value:bir})
        if(age != info.age) list.push({key:'age',value:age})
        if(constellation != info.constellation) list.push({key:'constellation',value:constellation})
        if(list.length >=1){
          await changeUserInfo( {list,iid:info.i_id} )
          this.$message.success("修改成功")
          this.CHANGE_INFO(list)
        }
        this.change.bir = false
      }
    },
  },
  computed: {
    years() {
      const currYear = new Date().getFullYear()
      const lastYear = 1960
      console.log(Array.from({ length: currYear - lastYear + 1 }, (_, index) => lastYear + index).reverse())
      return Array.from({ length: currYear - lastYear + 1 }, (_, index) => lastYear + index).reverse()
    },
    months(){
      let length
      const newdate = new Date()
      if(newdate.getFullYear() === this.bir.y)
        length = newdate.getMonth() + 1
      else length = new Date(this.bir.y,0,0).getMonth() + 1
      return Array.from({length}, (_, index) =>
        index<9? '0'+(index + 1):index + 1
      )
    },
    days() {
      let length
      const newdate = new Date()
      if(newdate.getFullYear() === this.bir.y && newdate.getMonth()+1 === this.bir.m)
        length = newdate.getDate()
      else length = new Date(this.bir.y, this.bir.m, 0).getDate()
      return Array.from({length}, (_, index) =>
        index<9? '0'+(index + 1):index + 1
      )
    },
    age(){
      const { y,m,d } = this.bir
      return parseInt((new Date() - new Date(y,m-1,d))/(1000*60*60*24*365))
    },
    constellation(){
      const { m,d } = this.bir
      const star = '摩羯水瓶双鱼白羊金牛双子巨蟹狮子处女天秤天蝎射手摩羯'
      const arr = [20,19,21,20,21,22,23,23,23,24,23,22]
      return star.substr(m*2 - (d<arr[m-1]?2:0),2)
    }
  },
}